package com.ly.algorithm.leetcode.linked;

/**
 * @Classname Problem82
 * @Description
 *
 * 给定一个排序链表，删除所有含有重复数字的节点，只保留原始链表中 没有重复出现 的数字。
 *
 * 示例 1:
 *
 * 输入: 1->2->3->3->4->4->5
 * 输出: 1->2->5
 * 示例 2:
 *
 * 输入: 1->1->1->2->3
 * 输出: 2->3
 *
 *
 * @Date 2020/12/15 20:24
 * @Author 冷心影翼
 */
public class Problem82 {

	public static void main(String[] args) {
		Solution82 solution82 = new Solution82();
		ListNode listNode = ListNode.init(new int[]{1, 2, 3, 3, 4, 4, 5});
		solution82.deleteDuplicates(listNode).printRes();
	}
}


/*
 * 输入: 1->2->3->3->4->4->5
 * 输出: 1->2->5
 */
class Solution82 {
	public ListNode deleteDuplicates(ListNode head) {
		ListNode preHead = new ListNode(-1);
		preHead.next = head;
		ListNode pre = preHead;
		ListNode cur = head;
		while (cur!=null) {
			boolean flag = false;
			while (cur.next!=null && cur.next.val == cur.val) {
				cur.next = cur.next.next;
				flag = true;
			}
			if(flag)
				pre.next = cur.next;
			else
				pre = pre.next;
			cur = cur.next;
		}
		return preHead.next;
	}
}